package com.ghsc.ghxc.jsb.ui.message.view

import android.os.CountDownTimer
import android.text.InputType
import android.text.TextUtils
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.api.ApiMarket
import com.ghsc.ghxc.jsb.config.PasswordEditText
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.ext.http.request
import com.lxj.xpopup.core.CenterPopupView


/**
 * 项目：GuoHuaMall
 * 包名：com.ghsc.ghxc.ui.my.view
 * 类名：PayPwdCodePopViiew
 * 创建时间：2023/8/3 00:02
 * 创建人 @author： xiaolin
 * 描述 ：Todo... 兑换 支付密码和验证码弹窗
 */
class PayPwdCodePopViiew  (context: AppCompatActivity, code:Int,isSendCode:Boolean,isSendPwd:Boolean, clickListener: SublickListener) : CenterPopupView(context) {

    override fun getImplLayoutId() = R.layout.pw_pay_pwd_code
    private var clickListener: SublickListener? = null



    interface SublickListener {
        fun onAdopt(etCode:String,pwdStr: String)
    }

    init {
        this.clickListener = clickListener
    }
    private val mContext = context
    private val code=code

    private val isSendCode=isSendCode
    private val isSendPwd=isSendPwd

//    private var tvBtnCode: () -> Unit = tvBtnCode

    var tvBtnCode: TextView?=null
    var myCountDownTimer: MyCountDownTimer? = null


    var etCode : TextView?=null
    var pwd : PasswordEditText?=null

    var tvBtnNext : TextView?=null
    var tvPwdXian:TextView?=null

    var llPhone1: LinearLayout?=null
    var llPhone2:LinearLayout?=null
    var tvPhoneXian:TextView?=null

//    var smsContent = SmsContentUtil(mContext, Handler(), pwd)


    override fun onCreate() {
        super.onCreate()
        // 注册短信变化监听
//        mContext.contentResolver.registerContentObserver(Uri.parse("content://sms/"), true, smsContent )
        var tvPhont= findViewById<TextView>(R.id.tvPhont)
         etCode= findViewById<EditText>(R.id.etCode)
        tvBtnCode= findViewById(R.id.tvBtnCode)


        llPhone1= findViewById(R.id.llPhone1)
        llPhone2= findViewById(R.id.llPhone2)
        tvPhoneXian= findViewById(R.id.tvPhoneXian)

        tvPwdXian= findViewById<TextView>(R.id.tvPwdXian)
         pwd= findViewById<PasswordEditText>(R.id.etPwd)
        tvBtnNext = findViewById<TextView>(R.id.tvBtnNext)
        etCode!!.inputType=InputType.TYPE_CLASS_NUMBER

        if(isSendCode){

            llPhone1!!.visibility=android.view.View.VISIBLE
            llPhone2!!.visibility=android.view.View.VISIBLE
            tvPhoneXian!!.visibility=android.view.View.VISIBLE
        }else{
            llPhone1!!.visibility=android.view.View.GONE
            llPhone2!!.visibility=android.view.View.GONE
            tvPhoneXian!!.visibility=android.view.View.GONE
        }
        if (isSendPwd){
            tvPwdXian!!.visibility=android.view.View.VISIBLE
            pwd!!.visibility=android.view.View.VISIBLE
        }else{
            tvPwdXian!!.visibility=android.view.View.GONE
            pwd!!.visibility=android.view.View.GONE
        }

        var userinfo= UserManager.getUserInfo()
        tvPhont.text=userinfo!!.mobile

        findViewById<TextView>(R.id.tvBtnClose).setOnClickListener {
            dismiss()
        }
        findViewById<ImageView>(R.id.imgBtnClose).setOnClickListener {
            dismiss()
        }
        tvBtnCode!!.setSingleClickListener {
            val map: HashMap<String, Any> = HashMap()
            map["mobile"] = tvPhont.text.toString().trim()
            map["type"]=code
            val json=  GsonUtil.mapToJsonStrObjEncryption(map)
            mContext.request({ ApiMarket.service.onSendCodeData(json!!) }, { data ->
                if (myCountDownTimer==null)
                    myCountDownTimer = MyCountDownTimer(60000, 1000)
                myCountDownTimer?.start()
            })
        }
        tvBtnNext!!.setSingleClickListener {
            if (isSendCode){
                if (TextUtils.isEmpty(etCode!!.text.toString().trim())){
                    showToast("请输入验证码")
                    return@setSingleClickListener
                }
            }
            if(isSendPwd){
                if (TextUtils.isEmpty(pwd!!.text.toString().trim())){
                    showToast("请输入支付密码")
                    return@setSingleClickListener
                }
            }
            clickListener!!.onAdopt(etCode!!.text.toString().trim(),pwd!!.text.toString().trim())
            dismiss()
        }
    }

    /**
     * 倒计时函数
     */
    inner class MyCountDownTimer(millisInFuture: Long, countDownInterval: Long) :
        CountDownTimer(millisInFuture, countDownInterval) {
        //计时过程
        override fun onTick(l: Long) {
            //防止计时过程中重复点击
            tvBtnCode!!.isClickable = false
            tvBtnCode!!.text = (l / 1000).toString() + "s"
        }

        //计时完毕的方法
        override fun onFinish() {
            //设置可点击
            tvBtnCode!!.text = resources.getString(R.string.time_again_str)
            tvBtnCode!!.isClickable = true
        }
    }



}